<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <!-- <el-form-item label="序号" prop="heathCode">
        <el-input
          v-model="queryParams.heathCode"
          placeholder="请输入序号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机器人id" prop="robotId">
        <el-input
          v-model="queryParams.robotId"
          placeholder="请输入机器人id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->

      <el-form-item label="机器人编号" prop="robotCode">
        <el-input
          class="!w-240px"
          v-model="queryParams.robotCode"
          placeholder="请输入机器人编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机器人名称" prop="robotName">
        <el-input
          class="!w-240px"
          v-model="queryParams.robotName"
          placeholder="请输入机器人名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="客户" prop="customer">
        <el-input
          v-model="queryParams.customer"
          placeholder="请输入客户"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障描述" prop="faultRemark">
        <el-input
          v-model="queryParams.faultRemark"
          placeholder="请输入故障描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地区" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入地区"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="机器人类型" prop="robotType">
        <el-select
          v-model="queryParams.robotType"
          placeholder="请选择机器人类型"
          clearable
          class="!w-240px"
        >
          <el-option label="智能盘点机器人" value="1" />
          <el-option label="智能还书系统" value="2" />
          <el-option label="智能导览机器人" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="故障严重程度" prop="severity">
        <el-select
          v-model="queryParams.severity"
          placeholder="请选择故障严重程度"
          clearable
          class="!w-240px"
        >
          <el-option label="轻微" value="1" />
          <el-option label="一般" value="2" />
          <el-option label="严重" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="handleStatus">
        <el-select
          v-model="queryParams.handleStatus"
          placeholder="请选择处理状态"
          clearable
          class="!w-240px"
        >
          <el-option label="未处理" value="0" />
          <el-option label="处理中" value="1" />
          <el-option label="已处理" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="检测时间" prop="findTime">
        <el-date-picker
          v-model="queryParams.findTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"
          ><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
        >
        <el-button @click="resetQuery"
          ><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button
        >
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['health:robot-fault-monitor:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['health:robot-fault-monitor:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column label="序号" align="center" type="index" width="60px" />
      <el-table-column label="机器人类型" align="center" prop="robotType">
        <template #default="{ row }">
          {{ rbtType(row.robotType) }}
        </template>
      </el-table-column>
      <el-table-column label="机器人编号" align="center" prop="robotCode" />
      <el-table-column label="机器人名称" align="center" prop="robotName" />
      <el-table-column label="客户" align="center" prop="customer" />
      <el-table-column label="故障描述" align="center" prop="faultRemark" />
      <el-table-column label="地区" align="center" prop="area" />

      <el-table-column
        label="故障严重程度"
        align="center"
        prop="severity"
        width="180px"
      >
        <template #default="{ row }">
          {{ severityType(row.severity) }}
        </template>
      </el-table-column>
      <el-table-column label="处理状态 " align="center" prop="handleStatus">
        <template #default="{ row }">
          {{ handleStatusType(row.handleStatus) }}
        </template>
      </el-table-column>
      <el-table-column
        label="检测时间"
        align="center"
        prop="findTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['health:robot-fault-monitor:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['health:robot-fault-monitor:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RobotFaultMonitorForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from "@/utils/dict";
import { dateFormatter } from "@/utils/formatTime";
import download from "@/utils/download";
import {
  RobotFaultMonitorApi,
  RobotFaultMonitorVO,
} from "@/api/health/robotfaultmonitor";
import RobotFaultMonitorForm from "./RobotFaultMonitorForm.vue";

/** 故障监测 列表 */
defineOptions({ name: "RobotFaultMonitor" });

const message = useMessage(); // 消息弹窗
const { t } = useI18n(); // 国际化

const loading = ref(true); // 列表的加载中
const list = ref<RobotFaultMonitorVO[]>([]); // 列表的数据
const total = ref(0); // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  heathCode: undefined,
  robotId: undefined,
  robotType: undefined,
  robotCode: undefined,
  robotName: undefined,
  customer: undefined,
  faultRemark: undefined,
  area: undefined,
  severity: undefined,
  handleStatus: undefined,
  findTime: [],
  createTime: [],
});
const queryFormRef = ref(); // 搜索的表单
const exportLoading = ref(false); // 导出的加载中
//枚举
const rbtType = (status) => {
  if (status == 1) {
    return "智能导览机器人";
  } else if (status == 2) {
    return "智能还书系统";
  } else if (status == 3) {
    return "智能盘点机器人";
  }
};
const severityType = (status) => {
  const STATUSES = [
    { severity: 1, name: "轻微" },
    { severity: 2, name: "一般" },
    { severity: 3, name: "严重" },
  ];

  const matched = STATUSES.find((item) => item.severity === status);
  return matched ? matched.name : "";
};
const handleStatusType = (status) => {
  console.log(status, "status");

  const STATUSES = [
    { handleStatus: 0, name: "未处理" },
    { handleStatus: 1, name: "处理中" },
    { handleStatus: 2, name: "已处理" },
  ];

  const matched = STATUSES.find((item) => item.handleStatus === status);
  return matched ? matched.name : "";
};
/** 查询列表 */
const getList = async () => {
  loading.value = true;
  try {
    const data =
      await RobotFaultMonitorApi.getRobotFaultMonitorPage(queryParams);
    list.value = data.list;
    total.value = data.total;
  } finally {
    loading.value = false;
  }
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields();
  handleQuery();
};

/** 添加/修改操作 */
const formRef = ref();
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id);
};

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm();
    // 发起删除
    await RobotFaultMonitorApi.deleteRobotFaultMonitor(id);
    message.success(t("common.delSuccess"));
    // 刷新列表
    await getList();
  } catch {}
};

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm();
    // 发起导出
    exportLoading.value = true;
    const data =
      await RobotFaultMonitorApi.exportRobotFaultMonitor(queryParams);
    download.excel(data, "故障监测.xls");
  } catch {
  } finally {
    exportLoading.value = false;
  }
};

/** 初始化 **/
onMounted(() => {
  getList();
});
</script>
